package cn.edu.xhu.library_management.book.utils;

import cn.edu.xhu.library_management.book.config.MinioConfig;
import io.minio.*;
import io.minio.errors.*;
import io.minio.messages.DeleteObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @Author: MMJ
 * @Date: 2023 - 03 - 21
 * @Description: Minio工具类
 */
@Component
public class MinioUtils {
    @Autowired
    private MinioClient minioClient;
    @Autowired
    private MinioConfig minioConfig;
    @Value("${minio.bucket}")
    public String bucketName;

    @Value("${minio.urlPrefix}")
    public String urlPrefix;

    /* 文件操作 */
    public String upload(MultipartFile file) {
        // 文件名为 UUID + "-" +文件名
        // eg: *-text.txt
        // 获取文件名
        String filename = file.getOriginalFilename();
        UUID uuid = UUID.randomUUID();
        /* 上传的文件名 */
        String upFileName = uuid.toString().replaceAll("-","") + "-" + filename;
        try {
            /* 上传文件 */
            InputStream inputStream = file.getInputStream();
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(bucketName)
                    .object(urlPrefix + upFileName)
                    .stream(inputStream, -1, 10485760)
                    .build());
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 返回地址
        return "/" + bucketName + urlPrefix + upFileName;
    }

    /**
     * 上传指定路径(文件)
     * @param file 文件对象
     * @param urlPrefix 路径
     * @return
     */
    public String upload(MultipartFile file,String urlPrefix) {
        // 文件名为 UUID + "-" +文件名
        // eg: *-text.txt
        // 获取文件名
        String filename = file.getOriginalFilename();
        UUID uuid = UUID.randomUUID();
        /* 上传的文件名 */
        String upFileName = uuid.toString() + "-" + filename;

        try {
            /* 上传文件 */
            InputStream inputStream = file.getInputStream();
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(bucketName)
                    .object(urlPrefix + upFileName)
                    .stream(inputStream, -1, 10485760)
                    .build());
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 返回地址
        return minioConfig.getServer() + ":" + minioConfig.getPort() + "/" + bucketName + urlPrefix + upFileName;
    }
    /**
     * 通过文件名删除文件对象
     * @param fileName 文件名
     */
    public void removeFile(String fileName) {
        try {
            minioClient.removeObject(RemoveObjectArgs.builder()
                            .bucket(bucketName)
                            .object(urlPrefix + fileName)
                    .build());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
